var row_hid=null;
var row_pid=null;
var row_tid=null;
$(document).ready(function(){
	var hId=$('#hId').val();
	var tId=$('#tId').val();
	var pId=$('#pId').val();
	row_hid=hId;
	row_tid=tId;
	row_pid=pId;
	initForm(hId);
	//事件绑定
	eventBind();
});
//事件绑定
function eventBind(){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	$('#save').on('click',function(){
		saveRevPatient(row_tid,row_pid);
	});
}
function initForm(hId){
	$('#revForm').validation();
	//初始化所属医院级拉框
	initDepartmentSel(hId,null,null);
}

//初始化所属科室下拉框
function initDepartmentSel(hosId,defaultVal,defaultDocVal){
	if(hosId){
		$.getApiClient('/baseInfo/departments').setQuery({hospital_id:hosId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("department",[{"id": "", "department_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#department').multipleSelect({width:'100%', single: true});
		});
		initAssignDocSel(defaultVal,defaultDocVal);
	}else{
		multipleSelectData("department",[{"id": "", "department_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
		$('#department').multipleSelect({width:'100%', single: true});
	}
	//科室医生下拉框联动
	$('#department').change(function(){
		var deptId=$(this).children('option:selected').val();
		initAssignDocSel(deptId,defaultDocVal);
	}).change();
}

function initAssignDocSel(deptId,defaultDocVal){
	if(deptId){
		$.getApiClient('/consultation/doctors/{department_id}').setQuery({department_id:deptId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("user",[{"id": "", "user_name": "请选择"}].concat(data.payload), defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
			$('#user').multipleSelect({width:'100%', single: true});
		});
	}else{
		multipleSelectData("user",[{"id": "", "user_name": "请选择"}], defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
		$('#user').multipleSelect({width:'100%', single: true});
	}
}

//接诊操作
function saveRevPatient(row_tid,row_pid){
	if(!$("#revForm").validationForm()){
		return;
	}
	$.getApiClient('/transfer/review/{id}').setQuery({id:row_tid}).put('#revForm', function(params){
		return $.extend(params, {
			assign_did:$('#department').multipleSelect('getSelects','value ')[0],
			assign_docid: $('#user').multipleSelect('getSelects','value ')[0],
			patient_id:row_pid,
			id:row_tid
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','审核成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}